package com.pactera.madp.cp.handler;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 应用id处理类
 */
@Component
public class AgentContextHolderFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        //Integer agentId = request.getHeader(Constant.MAPD_AGENT_ID);
        Integer agentId = 1000018;
        if(!StringUtils.isEmpty(agentId)){
            AgentContextHolder.setAgentId(agentId);
        }
        filterChain.doFilter(request,response);
       AgentContextHolder.remove();
    }
}
